package com.hsnn.medstgmini.pro.project.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.pro.project.dao.ProdBidCatalogDao;
import com.hsnn.medstgmini.pro.project.model.form.ProdBidCatalogForm;
import com.hsnn.medstgmini.util.MapToBeanUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 生产企业招标目录管理
 * @author 蔡春龙
 * @date 2015年8月24日
 */
@Repository
public class ProdBidCatalogDaoImpl implements ProdBidCatalogDao {

	@Resource(name = "sessionFactory")
	private SessionFactory factory;

	@Autowired
	private HibernateSqlUtil sqlUtil;

	/**
	 * @category 生产企业获取招标目录列表
	 * @date 2015年8月25日
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Pagination getBidCatalogList(Pagination page) {
		StringBuffer sb = new StringBuffer();
		sb.append(" SELECT b.proj_id projId, b.name projName, c.qualityspec_id qualityspecId, c.qualityspec_code qualityspecCode, c.qualityspec_name qualityspecName,  ddf.form_name form, dds.spec_name spec, d.drugcatalog_name drugcatalogName, d.attribute, e.qualityspec_class qualityspecClass, e.qualityspec_index qualityspecIndex FROM bid_qualityspec a JOIN bid_project b ON a.proj_id = b.proj_id JOIN std_drugqualityspec c ON a.qualityspec_id = c.qualityspec_id  join dic_drugform ddf on ddf.form_id=c.form join dic_drugspec dds on dds.spec_id=c.spec   JOIN std_drugcatalog d ON a.drugcatas_id = d.drugcatalog_id LEFT JOIN bid_drugcatas e ON a.qualityspec_id = e.qualityspec_id AND a.drugcatas_id = e.drugcatalog_id WHERE a.status = :status ");

		ProdBidCatalogForm form = (ProdBidCatalogForm) page.getForm();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Status.enabled.getKey());

		if (form != null) {
			String projId = form.getProjId();
			if (StringUtils.isNotBlank(projId)) {
				sb.append(" AND b.proj_id = :projId");
				params.put("projId",projId);
			}

			String qualityspecName = form.getQualityspecName();
			if (StringUtils.isNotBlank(qualityspecName)) {
				sb.append(" AND c.qualityspec_name LIKE :qualityspecName");
				params.put("qualityspecName", "%" + qualityspecName + "%");
			}

			String drugcatalogName = form.getDrugcatalogName();
			if (StringUtils.isNotBlank(drugcatalogName)) {
				sb.append(" AND d.drugcatalog_name LIKE :drugcatalogName");
				params.put("drugcatalogName", "%" + drugcatalogName + "%");
			}
		}
		
		sb.append(" ORDER BY b.proj_id ");

		sqlUtil.getSqlPageMapList2(sb.toString(), params, page);

		List<Map<String, Object>> list = (List<Map<String, Object>>) page
				.getRows();

		if (list != null) {
			List<ProdBidCatalogForm> formList = MapToBeanUtil.mapToBean(list,
					ProdBidCatalogForm.class);
			page.setRows(formList);
		}

		return page;
	}

}
